﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietImage.Models;
using VietImage.ViewModels;
using VietImage.Roles;

namespace VietImage.Controllers
{
    public class LightboxController : Controller
    {
        //
        // GET: /Lightbox/

        private void GetLanguage()
        {
            HttpCookie languageCookie = null;
            if (languageCookie == null)
            {
                languageCookie = new HttpCookie("Language");
                languageCookie.Value = "2";
            }

            languageCookie.Expires = DateTime.Now.AddMonths(1);
            //Response.SetCookie(languageCookie);
            ViewData["Language"] = int.Parse(languageCookie.Value);
        }

        private String GetErrorResult(List<ErrorItem> errorlist)
        {
            String result = "";
            if (errorlist != null && errorlist.Count > 0)
            {
                result = "Error:\n";
                foreach (ErrorItem error in errorlist)
                {
                    result += "- " + error + "\n";
                }
            }
            else
            {
                result = "Success!";
            }
            return result;
        }
        
        public ActionResult Index(int id = 0, String opt = "Thumbnail")
        {
            GetLanguage();
            RegisteredUserRole role = (RegisteredUserRole)Session["Role"];
            if (role is CustomerRole)
            {
                List<Lightbox> myLightboxes = Lightbox.GetListAvailableOfUser(role.UserInfo.ID);
                ViewData["LightboxList"] = myLightboxes;
                if (myLightboxes != null && myLightboxes.Count > 0)
                {
                    var x = from lb in myLightboxes
                            where lb.ID == id
                            select lb;
                    ViewData["Lightbox"] = (x != null && x.Count() == 1) ? x.First() : myLightboxes[0];
                }
            }
            return View();
        }

        [HttpPost]
        public ActionResult Detail(int id = 0, String opt = "Thumbnail")
        {
            GetLanguage();
            RegisteredUserRole role = (RegisteredUserRole)Session["Role"];
            if (id != 0 && role is CustomerRole)
            {
                Lightbox lb = Lightbox.GetLightbox(id);
                if (lb.ID_User == role.UserInfo.ID)
                {
                    ViewData["Lightbox"] = lb;
                }
            }
            return PartialView("LightboxControl");
        }

        public ActionResult ShowAddToLightbox(int id = 0)
        {
            GetLanguage();
            RegisteredUserRole role = (RegisteredUserRole)Session["Role"];
            Image img = Image.GetImage(id);
            if (img != null && img != null && role is CustomerRole)
            {
                List<Lightbox> lst = Lightbox.GetListAvailableOfUser(role.UserInfo.ID);
                ViewData["LightboxList"] = lst;
                ViewData["Image"] = img;
            }
            return PartialView("AddToLightbox");
        }

        public String AddToLightBox(int id = 0, int imgid = 0)
        {
            GetLanguage();
            RegisteredUserRole role = (RegisteredUserRole)Session["Role"];
            List<ErrorItem> errorlist = null;
            Image img = Image.GetImage(imgid);
            if (id != 0 && img != null && role is CustomerRole)
            {
                Lightbox lb = Lightbox.GetLightbox(id);
                if (lb.ID_User == role.UserInfo.ID)
                {
                    errorlist = lb.InsertImage(imgid, role.UserInfo.ID, (int)ViewData["Language"]);
                }
            }
            return GetErrorResult(errorlist);
        }

        public String AddToNewLightBox(String name = "", int imgid = 0)
        {
            GetLanguage();
            RegisteredUserRole role = (RegisteredUserRole)Session["Role"];
            List<ErrorItem> errorlist = null;
            if (name.Trim() != "" && role is CustomerRole)
            {
                Lightbox lb = new Lightbox();
                lb.ID_User = role.UserInfo.ID;
                lb.Name = name.Trim();
                lb.ID_Status = 1;
                errorlist = ((CustomerRole)role).CreateLightBox(lb);
                if (errorlist != null && errorlist.Count > 0)
                {
                    return GetErrorResult(errorlist);
                }
                errorlist = ((CustomerRole)role).InsertImageLightBox(imgid, lb.ID);
            }
            return GetErrorResult(errorlist);
        }

        public String CreateOrRenameLightbox(string type = "create", string name = "", int id = 0)
        {
            GetLanguage();
            RegisteredUserRole role = (RegisteredUserRole)Session["Role"];
            List<ErrorItem> errorlist = null;
            if (name.Trim() != "" && role is CustomerRole)
            {
                if (id != 0 && type != "create")
                {
                    Lightbox lb = Lightbox.GetLightbox(id);
                    lb.Name = name.Trim();
                    errorlist = ((CustomerRole)role).EditLightBox(lb);
                }
                else
                {
                    Lightbox lb = new Lightbox();
                    lb.Name = name.Trim();
                    lb.ID_User = role.UserInfo.ID;
                    lb.ID_Status = 1;
                    errorlist = ((CustomerRole)role).CreateLightBox(lb);
                }
            }
            return GetErrorResult(errorlist);
        }

        public String DeleteLightbox(int id = 0)
        {
            GetLanguage();
            RegisteredUserRole role = (RegisteredUserRole)Session["Role"];
            List<ErrorItem> errorlist = null;
            if (id != 0 && role is CustomerRole)
            {
                errorlist = ((CustomerRole)role).DeleteLightBox(id);
            }
            return GetErrorResult(errorlist);
        }

        public String RemoveFromLightbox(int lbID)
        {
            Helpers.ControllerHelper.GetLanguage(ViewData, Request, Response);
            RegisteredUserRole role = (RegisteredUserRole)Session["Role"];
            List<ErrorItem> errorlist = null;
            if (lbID != 0 && role is CustomerRole)
            {
                foreach (String k in Request.Form.AllKeys)
                {
                    if (k.Contains("chkimg"))
                    {
                        int imgID = int.Parse(k.Replace("chkimg", "").Trim());
                        errorlist = ((CustomerRole)role).RemoveImageLightBox(imgID, lbID);
                    }
                }
            }
            return Helpers.ControllerHelper.GetErrorResult(errorlist);
        }

        public String CopyToLightbox(int lbID)
        {
            Helpers.ControllerHelper.GetLanguage(ViewData, Request, Response);
            RegisteredUserRole role = (RegisteredUserRole)Session["Role"];
            List<ErrorItem> errorlist = null;
            if (lbID != 0 && role is CustomerRole)
            {
                foreach (String k in Request.Form.AllKeys)
                {
                    if (k.Contains("chkimg"))
                    {
                        int imgID = int.Parse(k.Replace("chkimg", "").Trim());
                        errorlist = ((CustomerRole)role).InsertImageLightBox(imgID, lbID);
                    }
                }
            }
            return Helpers.ControllerHelper.GetErrorResult(errorlist);
        }
    }
}
